package com.kycxika.app.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.kycxika.app.R;
import com.kycxika.app.activity.base.BaseFragment;
import com.kycxika.app.divider.DividerItemDecoration;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/29.
 */

public class ListFragment extends BaseFragment {

    RecyclerView mRecyclerView;
    private static final String KEY = "key";
    private String title = "测试";

    ArrayList<String> mDatas = new ArrayList<>();
    private ItemAdapter mAdapter;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private View view;

    public static ListFragment newInstance(String title) {
        ListFragment fragment = new ListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(KEY, title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = View.inflate(getContext(), R.layout.fragment_list, null);
        initView();
        return view;
    }

    public void initView() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            title = arguments.getString(KEY);
        }
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setEnabled(false);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        DividerItemDecoration itemDecoration = new DividerItemDecoration(getContext(),
                LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);
        mRecyclerView.setLayoutManager(layoutManager);

        for (int i = 0; i < 50; i++) {
            String s = String.format("我是第%d个" + title, i);
            mDatas.add(s);
        }

        mAdapter = new ItemAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mSwipeRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(getContext(), "刷新完成", Toast.LENGTH_SHORT).show();
                    }
                }, 1200);
            }
        });


    }

    public void tooglePager(boolean isOpen) {
        if (isOpen) {
            setRefreshEnable(false);
            scrollToFirst(false);
        } else {
            setRefreshEnable(true);
        }
    }

    public void scrollToFirst(boolean isSmooth) {
        if (mRecyclerView == null) {
            return;
        }
        if (isSmooth) {
            mRecyclerView.smoothScrollToPosition(0);
        } else {
            mRecyclerView.scrollToPosition(0);
        }
    }

    public void setRefreshEnable(boolean enabled) {
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setEnabled(enabled);
        }

    }

    public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder>{

        @Override
        public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(getContext()).inflate(R.layout.free_list_item,null));
            return itemViewHolder;
        }

        @Override
        public void onBindViewHolder(ItemViewHolder holder, int position) {
            holder.list_title.setText(mDatas.get(position));
        }

        @Override
        public int getItemCount() {
            return mDatas.size() > 0 ? mDatas.size() : 0 ;
        }


        public class ItemViewHolder extends RecyclerView.ViewHolder {

            private ImageView title_img;
            private TextView list_title;

            public ItemViewHolder(View view){
                super(view);
                title_img = (ImageView) view.findViewById(R.id.title_img);
                list_title = (TextView) view.findViewById(R.id.list_title);
            }
        }
    }

}
